/****************************************************************************
  CLASS:				MiscGaugeViewInspector
  
  See the interface file for more information on this class.
 ****************************************************************************/
 
#import <appkit/appkit.h>
#import "MiscGaugeView.subproj/MiscGaugeView.h"
#import "MiscGaugeViewInspector.h"


@implementation MiscGaugeViewInspector

- init
{
    char buf[MAXPATHLEN+1];
    id bundle;
    
    [super init];
    
	// Load the inspector's nib file.
    bundle = [NXBundle bundleForClass:[MiscGaugeView class]];
    [bundle getPath: buf forResource: "MiscGaugeViewInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	return self;
}

	
- attribChanged:sender
{
	// One of the attributes changed, so find out which one and update us.
	// Syncronize the slider with it's matching form cell. The sender is
	// the attribForm. After we set the attrib. we check back with the
	// object and see what the value was really set to (since it may have
	// been illegal) and update the form cell.
	int selIndex = [sender selectedRow];
	
	switch (selIndex)
	{
		case 0:	// Start angle changed.
			[object setStartAngle: [sender floatValueAt: 0] ];
			[sender setFloatValue: [object startAngle] at: 0];
			break;
			
		case 1:	// Angle range changed.
			[object setAngleRange: [sender floatValueAt: 1] ];
			[sender setFloatValue: [object angleRange] at: 1];
			break;
			
		case 2: // Hand ratio changed.
			[object setHandRatio: [sender floatValueAt: 2] ];
			[sender setFloatValue: [object handRatio] at: 2];
			break;

		case 3:	// Tick ratio changed.
			[object setTickRatio: [sender floatValueAt: 3] ];
			[sender setFloatValue: [object tickRatio] at: 3];
			break;

		case 4: // Tick interval changed.
			[object setTickInterval: [sender intValueAt: 4] ];
			[sender setFloatValue: [object tickInterval] at: 4];
			break;
	 }		
	
	// Tick interval has no slider so don't send a message.
	if (selIndex != 4)
		[ [sliderMatrix cellAt: selIndex : 0] 
						setFloatValue: [sender floatValueAt: selIndex] ];

    return [super ok: sender];
}


- sliderChanged: sender
{
	// A slider changed, so update the inspected object and 
	// the attribForm. The sender is the sliderMatrix.
	Cell *selectedCell = [sender selectedCell];
	
	switch ([sender selectedTag])
	{
		case 0:	// Start angle changed.
			[object setStartAngle: [selectedCell floatValue] ];
			break;
			
		case 1:	// Angle range changed.
			[object setAngleRange: [selectedCell floatValue] ];
			break;
			
		case 2: // Hand ratio changed.
			[object setHandRatio: [selectedCell floatValue] ];
			break;

		case 3:	// Tick ratio changed.
			[object setTickRatio: [selectedCell floatValue] ];
			break;
	 }

	// Set the matching attribute in the form, so it and it's
	// slider have the same value.
	[attribsForm setFloatValue: [selectedCell floatValue] 
					at: [sender selectedTag] ];
	return [super ok: sender];
}


- backgroundColorChanged:sender
{
	// The sender is a NXColorWell.
	[object setBackgroundColor: [sender color] ];
    return [super ok: sender];
}


- gaugeColorChanged:sender
{
	// The sender is a NXColorWell.
	[object setGaugeColor: [sender color] ];
	return [super ok: sender];
}


- textColorChanged:sender
{
	// The sender is the NXColorWell representing text color.
	[object setTextColor: [sender color] ];
    return [super ok: sender];
}


- valueChanged:sender
{
	// A value in the value form changed, so find out which one and
	// update the object. The sender is the valueForm.
	int selIndex = [sender selectedIndex];
	
	switch (selIndex)
	{
		case 0:	// Min value changed.
			[(MiscGaugeView *)object setMinValue: [sender floatValueAt: 0] ];
			// The max. value may have changed if it became less than the min.
			[sender setFloatValue: [(MiscGaugeView *)object maxValue] at: 1];
			break;
		
		case 1: // Max value changed.
			[(MiscGaugeView *)object setMaxValue: [sender floatValueAt: 1] ];
			// The min value may have changed if it became more than the max.
			[sender setFloatValue: [(MiscGaugeView *)object minValue] at: 0];
			break;
			
		case 2:	// Current value changed.
			[object setFloatValue: [sender floatValueAt: 2] ];
			break;
	 }
	 
	// Since any of the above could have changed the value of the gauge 
	// (or the new value wasn't acceptable..above or below the max and
	// min), get the current value from the object itself and update the
	// formcell.
	[sender setFloatValue: [object floatValue] at: 2];

    return [super ok: sender];
}


- titleFieldChanged: sender
{
	[object setTitle: [sender stringValue] ];
	return [super ok: sender];
}


- titlePositionChanged: sender
{
	switch ([sender selectedTag])
	{
		case 0:
			[object setTitlePosition: NX_ATTOP];
			break;
		
		case 1:
			[object setTitlePosition: NX_ATBOTTOM];
			break;
	 }
	return [super ok: sender];
}


- revert: sender
{
	// The place to ask the inspected object for all it's values, so we
	// can fill up the inspector's fields and color wells.

//	[backgroundColorWell setColor: [object backgroundColor] ];
	[gaugeColorWell setColor: [object gaugeColor] ];
	[textColorWell setColor: [object textColor] ];
	[titleField setStringValue: [object title] ];
	
	[valueForm setFloatValue: [(MiscGaugeView *)object minValue] at: 0];
	[valueForm setFloatValue: [(MiscGaugeView *)object maxValue] at: 1];
	[valueForm setFloatValue: [object floatValue] at: 2];
	
	[attribsForm setFloatValue: [object startAngle] at: 0];
	[attribsForm setFloatValue: [object angleRange] at: 1];
	[attribsForm setFloatValue: [object handRatio] at: 2];
	[attribsForm setFloatValue: [object tickRatio] at: 3];
	[attribsForm setIntValue: [object tickInterval] at: 4];

	[ [sliderMatrix cellAt: 0 : 0] setFloatValue: [object startAngle] ];
	[ [sliderMatrix cellAt: 1 : 0] setFloatValue: [object angleRange] ];
	[ [sliderMatrix cellAt: 2 : 0] setFloatValue: [object handRatio] ];
	[ [sliderMatrix cellAt: 3 : 0] setFloatValue: [object tickRatio] ];
	
	if ([object titlePosition] == NX_ATTOP)
		[titlePositionMatrix selectCellAt: 0 : 0];
	else
		[titlePositionMatrix selectCellAt: 1 : 0];
		
	return [super revert: sender];
}


- (BOOL)wantsButtons
{
	return NO;
}

@end


@implementation MiscGaugeViewInspector (NibInitialization)

- awakeFromNib
{
	// Set all the decimal places for the attributes.
	[[attribsForm cellAt: 0 : 0] setFloatingPointFormat: YES left: 3 right: 1];
	[[attribsForm cellAt: 1 : 0] setFloatingPointFormat: YES left: 3 right: 1];
	[[attribsForm cellAt: 2 : 0] setFloatingPointFormat: YES left: 1 right: 2];
	[[attribsForm cellAt: 3 : 0] setFloatingPointFormat: YES left: 1 right: 2];
	return self;
}		

@end

